package com.lottery.game.core.modules.service.blank.account.impl;

import java.util.Date;
import java.util.List;

import com.lottery.game.core.modules.service.game.SysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lottery.game.core.modules.entity.blank.acount.ReceivingBankAccountSec;
import com.lottery.game.core.modules.entity.user.GameUser;
import com.lottery.game.core.modules.entity.user.UpAndDownReport;
import com.lottery.game.core.modules.mapper.BlankAcountDao;
import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.app.UpAndDownReportService;
import com.lottery.game.core.modules.service.blank.account.BlankAcountService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

@Service
public class BlankAcountServiceImpl implements BlankAcountService {

	@Autowired
	private BlankAcountDao blankAcountDao;
	@Autowired
	private GameUserService gameUserService;
	@Autowired
	private UpAndDownReportService upAndDownReportService;
	@Autowired
	private SysDictService sysDictService;

	private String imageServerAddress;

	/**
	 * 用戶银行账户查询
	 */
	@Override
	public PageInfo<ReceivingBankAccountSec> findReceivingBankAccount(ReceivingBankAccountSec receivingBankAccount) {
		PageHelper.startPage(receivingBankAccount.getPage(), receivingBankAccount.getRows());
		List<ReceivingBankAccountSec> list = blankAcountDao.findReceivingBankAccount(receivingBankAccount);
		if (!CollectionUtils.isEmpty(list)){
			//拼接图片
			String imageServer = this.getImageServer();
			for (ReceivingBankAccountSec accountSec : list) {
				String iconPhoto = accountSec.getIconPhoto();
				String twoDimensionalCode = accountSec.getTwoDimensionalCode();
				accountSec.setIconPhoto(imageServer+iconPhoto);
				accountSec.setTwoDimensionalCode(imageServer+twoDimensionalCode);
			}
		}
		PageInfo<ReceivingBankAccountSec> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	/**
	 * 充值金额提交
	 */
	@Transactional
	@Override
	public int saveUp(UpAndDownReport upAndDownReport, String gameid) {
		int i  = 1;
		GameUser gameUser = gameUserService.get(gameid);
		UpAndDownReport report = new UpAndDownReport();
		report.setApplyType("1");
		report.setStatus("0");
		report.setNickname(upAndDownReport.getNickname());
		report.setPayType(upAndDownReport.getPayType());
		report.setApplyMoneyAmount(upAndDownReport.getApplyMoneyAmount());
		List<UpAndDownReport> list = upAndDownReportService.findList(report);
		if (list.size() > 0) {
			i = 1;
			//map.put("state", "0");
			//map.put("a", "充值正在处理中 请勿重复提交！");
			return i;
		}
		upAndDownReport.setApplyType("1");
		upAndDownReport.setApplyTypeContext("上分");
		upAndDownReport.setApplyTime(new Date());
		upAndDownReport.setStatus("0");
		upAndDownReport.setStatusContext("待审核");
		upAndDownReport.setGameuserId(gameid);
		upAndDownReport.setNickname(gameUser.getNickname());
		upAndDownReport.setPhotourl(gameUser.getPhotourl());
		upAndDownReport.setBankAccount(gameUser.getPhone());
		upAndDownReport.setRemarks(upAndDownReport.getCode());
		upAndDownReport.setGameReceivingBankAccountId(upAndDownReport.getGameReceivingBankAccountId());
		// upAndDownReport.
		upAndDownReportService.save(upAndDownReport);
		//map.put("state", "1");
		//map.put("a", "上传成功");
		i = 0;
		//map.put("state", "0");
		//map.put("a", "上传失败");
		return i;
	}
	private String getImageServer() {
		if (imageServerAddress != null) {
			return imageServerAddress;
		}
		return imageServerAddress = sysDictService.findImageServerAddress();
	}
}
